﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace App
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        
        }
        protected void Page_PreRender(object sender, EventArgs e)
        {
            try
            {
                string nick = Server.UrlDecode(Request.Cookies["user"]["userNick"]);
                string pass = Server.UrlDecode(Request.Cookies["user"]["userPass"]);

                if (nick != null && pass!= null)
                {
                    txtUser.Text = nick;
                    txtPassword.Attributes.Add("value", pass);
                }
            }
            catch (Exception)
            {
                
            }
            try
            {
                if (Server.UrlDecode(Request.Cookies["passSent"].Value) != null)
                {
                    pnlInfo.Visible = true;
                    lblInfo.Text = Server.UrlDecode(Request.Cookies["passSent"].Value);
                    Response.Cookies["passSent"].Expires = DateTime.Now.AddYears(-1);
                }
            }
            catch (Exception)
            {
            }
            try
            {
                if (Server.UrlDecode(Request.Cookies["registeredUser"].Value) != null)
                {
                    pnlInfo.Visible = true;
                    lblInfo.Text = Server.UrlDecode(Request.Cookies["registeredUser"].Value);
                    Response.Cookies["registeredUser"].Expires = DateTime.Now.AddYears(-1);
                }
            }
            catch (Exception)
            {
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (MidApp.ManageUser.CompareInputToLogin(txtUser.Text,txtPassword.Text))
            {
                Common.SetLoggedUser(Page, MidApp.ManageUser.GetUserByNick(txtUser.Text));
                if (CheckBox1.Checked == true)
                {
                    Response.Cookies["user"]["userNick"] = Server.UrlEncode(txtUser.Text);
                    Response.Cookies["user"]["userPass"] = Server.UrlEncode(txtPassword.Text);

                }
                else
                {
                    Response.Cookies["user"].Expires = DateTime.Now.AddYears(-1);
                   
                }
                Response.Cookies["State"].Value = "1";

                if(MidApp.ManageIsAdministrator.IsAdministrator(Common.GetLoggedUser(this.Page).UserID))
                    Response.Redirect("Administration.aspx");
                else
                    Response.Redirect("EventsList.aspx");
            }
            else
            {
                lblInfo.Text = "Nesprávné údaje, nepřihlášeno";
            }
            pnlInfo.Visible = true;      
        }

        protected void ForgottenPass_Click(object sender, EventArgs e)
        {
            Response.Redirect("ForgottenPass.aspx");
        }
    }
}